﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;
using Utility.Json;
using Utility.Model;

namespace MsgCenter.Service.Helper
{
    class AliyunSmsHelper
    {
        private static readonly string product = AppConfigHelper.Aliyun_Product;

        private static readonly string domain = AppConfigHelper.Aliyun_Domain;

        private static readonly string accessKeyId = AppConfigHelper.Aliyun_AccessKeyID;

        private static readonly string accessKeySecret = AppConfigHelper.Aliyun_AccessKeySecret;

        public static string sendSms(string strMobile, string strCode, string strSendMessage, string strAliyun_TemplateCode = "")
        {
            DefaultProfile profile = DefaultProfile.GetProfile(AppConfigHelper.Aliyun_RegionID, accessKeyId, accessKeySecret);
            DefaultProfile.AddEndpoint(AppConfigHelper.Aliyun_RegionID, AppConfigHelper.Aliyun_RegionID, product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            SendSmsResponse sendSmsResponse = null;
            try
            {
                sendSmsRequest.PhoneNumbers = strMobile;
                sendSmsRequest.SignName = AppConfigHelper.Aliyun_SIGN;
                if (strAliyun_TemplateCode == "")
                {
                    sendSmsRequest.TemplateCode = AppConfigHelper.Aliyun_TemplateCode;
                }
                else
                {
                    sendSmsRequest.TemplateCode = strAliyun_TemplateCode;
                }
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string strTemplateParam = "";
                if (!strCode.IsNullOrEmpty())
                {
                    string[] templates = strCode.Split('|');
                    for (int i = 0; i < templates.Length; i++)
                    {
                        string key = "A" + (i + 1).ToIntString().PadLeft(4, '0');
                        dic.Add(key, templates[i]);
                    }
                }
                strTemplateParam = JsonHelper.ObjectToJson(dic);
                sendSmsRequest.TemplateParam = strTemplateParam;
                sendSmsRequest.OutId = DateTime.Now.DateTimeToyyyyMMddHHmmss();
                sendSmsResponse = acsClient.GetAcsResponse(sendSmsRequest);
                LogServiceHelper.WriteMessageLog("手机号(" + strMobile + ")发送短信成功，发送内容：" + strSendMessage + " \r\n 阿里接口返回值：Code:" + sendSmsResponse.Code + "  BizId:" + sendSmsResponse.BizId + " Message:" + sendSmsResponse.Message + " RequestId:" + sendSmsResponse.RequestId);
                return "Code:" + sendSmsResponse.Code + "  BizId:" + sendSmsResponse.BizId + " Message:" + sendSmsResponse.Message + " RequestId:" + sendSmsResponse.RequestId;
            }
            catch (ServerException ex)
            {
                LogServiceHelper.WriteMessageError(("发送短信失败(ServerException)" + ex.ErrorCode) ?? "", ex);
                return "Code:Error, Messge:" + ex.Message;
            }
            catch (ClientException ex2)
            {
                LogServiceHelper.WriteMessageError(("发送短信失败(ClientException)" + ex2.ErrorCode) ?? "", ex2);
                return "Code:Error, Messge:" + ex2.Message;
            }
        }
    }
}